package com.smartsaas.ugclivii.utils;

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.model.enums.AesKeyStrength;
import net.lingala.zip4j.model.enums.CompressionLevel;
import net.lingala.zip4j.model.enums.CompressionMethod;
import net.lingala.zip4j.model.enums.EncryptionMethod;

import java.io.File;

public class ZipUtils {
    public static String zip(String dirPath,String zipPath,String zipName,String pwd) throws ZipException {
        ZipParameters parameters = new ZipParameters();
        parameters.setCompressionMethod(CompressionMethod.DEFLATE);
        parameters.setCompressionLevel(CompressionLevel.NORMAL);
        parameters.setEncryptFiles(true);
        parameters.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
        parameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
        File target = new File(zipPath, zipName);
        String targetPath = target.getAbsolutePath();
        ZipFile zipFile = new ZipFile(targetPath,pwd.toCharArray());
        File dir = new File(dirPath);
        if (dir.exists()&&dir.isDirectory()) {
            File[] files = dir.listFiles();
            if (files!=null&&files.length>0){
                for (File f : files) {
                    if (f.isDirectory()){
                        zipFile.addFolder(f,parameters);
                    }else {
                        zipFile.addFile(f,parameters);
                    }
                }
            }
        }
        return targetPath;
    }
}
